<?php
/** .-------------------------------------------------------------------
 * |    Author: 熊伟洋 <chelious@foxmail.com>
 * |    WeChat: hello_McGrady
 * |           QQ: 434493420
 * |     Motto: Hungry & Humble
 * |---------------------------------------------------------------------
 * |    Copyright (c) 2012-2020, www.chelious.com. All Rights Reserved.
 * '-------------------------------------------------------------------*/

namespace app\admin\controller;

/**
 * Class Article
 * @package app\admin\controller
 * 文章管理控制器
 */
class Article extends Common
{
    private $model;

    public function __construct()
    {
        $this->model = new \system\model\Article();
        parent::__construct();
    }

    //文章列表载入
    public function index()
    {
        //分页
        $data = Db::table('article')->where('is_recycle', 0)->paginate(8);
        $page = $data->links();
        $data = $data->toArray();
//        p($data);
        foreach ($data as $k => $v) {
//            p($k);
//            p($v);

            $data[$k]['cname'] = Db::table('category')->where('cid', $v['category_cid'])->pluck('cname');
        }
        View::with(['data' => $data, 'page' => $page]);

        //载入模板
        return view();
    }

    //文章添加
    public function add()
    {
        //先获取category表的数据
        $cateData = Db::table('category')->get();
//        p($cateData);
        //把分类获取出来
        $cateData = Data::tree($cateData, 'cname');
//        p($cateData);
        //分配变量
        View::with('cateData', $cateData);
        //获取标签表的数据
        $tagData = Db::table('tag')->get();
//        p($tagData);
        View::with('tagData', $tagData);
        //页面加在完毕，开始处理post数据
        if (IS_POST) {
            if ($this->model->add()) {
                message('添加成功', u('index'), 'success');
            } else {
                message($this->model->getError(), 'back', 'error');
            }
        }

        //载入模板
        return view();
    }

    //删除到回收站
    public function del()
    {
        $aid = Q('get.aid');
//        p($aid);
        //修改is_recycle=0
        $this->model->where('aid', $aid)->update(['is_recycle' => 1]);
        message('已删除到回收站', u('recycle'), 'success');

    }

    //从回收站还原文章
    public function reduction()
    {
        $aid = Q('get.aid');
//        p($aid);
        //修改is_recycle=0
        $this->model->where('aid', $aid)->update(['is_recycle' => 0]);
        message('已还原', u('index'), 'success');

    }

    /**
     * @return mixed
     * 回收站
     * 1代表在回收站
     */
    public function recycle()
    {
        //分页
        $data = Db::table('article')->where('is_recycle', 1)->paginate(8);
        $page = $data->links();
        $data = $data->toArray();
//        p($data);
        foreach ($data as $k => $v) {
//            p($k);
//            p($v);

            $data[$k]['cname'] = Db::table('category')->where('cid', $v['category_cid'])->pluck('cname');
        }
        View::with(['data' => $data, 'page' => $page]);

        return view();
    }

    //编辑文章
    public function edit()
    {
        $aid = Q('get.aid');
//        p($aid);
        //获取旧数据
        $oldData = Db::table('article')->join('article_data', 'article.aid', '=', 'article_data.article_aid')
            ->where('aid', $aid)->first();
//        p($oldData);
        View::with('oldData', $oldData);
        //获得分类数据
        $cateData = Db::table('category')->get();
        $cateData = Data::tree($cateData, 'cname');
//        p($cateData);
        View::with('cateData', $cateData);
        //获得所有的标签数据
        $tagData = Db::table('tag')->get();
//        p($tagData);
        //获得选中的标签数据
        $tags = Db::table('article_tag')->where('article_aid', $aid)->lists('tag_tid');
//        p($tags);
        View::with('tagData', $tagData)->with('tags', $tags);

        if (IS_POST) {
            if($this->model->edit()){
                message('文章修改成功',u('index'),'success');
            }else{
                message($this->model->getError(),'back','error');
            }
        }

        return view();
    }

    //彻底删除
    public function delete()
    {
        $aid = Q('get.aid');
        //删除文章表
        $this->model->where('aid', $aid)->delete();
        //删除文章数据表
        $articleData = new \system\model\Article_data();
        $articleData->where('article_aid', $aid)->delete();
        //删除文章标签表
        $articleTag = new \system\model\Article_tag();
        $articleTag->where('article_aid', $aid)->delete();

        message('删除成功', u('recycle'), 'success');

    }


}
